﻿using System.Windows;
using Microsoft.Phone.Controls;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Media.Animation;
using System;
using System.Windows.Controls;

namespace WPNEXT.Views
{
    public partial class GuidePage : PhoneApplicationPage
    {
        private GestureListener _gestureListener;
        private int _currentIndex = 0;
        private double _playSeconds = 1000;
        private bool btnShow = false;

        public GuidePage()
        {
            InitializeComponent();
            
            this.Unloaded += GuidePage_Unloaded;
            this.Loaded += GuidePage_Loaded;
        }

        ~GuidePage()
        {
            Debug.WriteLine("销毁" + this.GetType().ToString());
        }

        void GuidePage_Loaded(object sender, RoutedEventArgs e)
        {
            HeartImage.Visibility = Visibility.Visible;
            PlayCanvasStoryBoard(this.HeartImage,149, 300, _playSeconds);

            _gestureListener = GestureService.GetGestureListener(this.ContainerCanvas);
            _gestureListener.DragStarted += OnDragStarted;
            _gestureListener.DragDelta += OnDragDelta;
            _gestureListener.DragCompleted += OnDragCompleted;
            _gestureListener.Flick += OnFlick;
           
        }
        void GuidePage_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_gestureListener != null)
            {
                _gestureListener.DragStarted -= OnDragStarted;
                _gestureListener.DragDelta -= OnDragDelta;
                _gestureListener.DragCompleted -= OnDragCompleted;
                _gestureListener.Flick -= OnFlick;
                _gestureListener = null;
            }
        }

        void OnDragStarted(object sender, DragStartedGestureEventArgs e)
        {
            
        }

        void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            PlaneProjection planeProjection = (PlaneProjection)ContainerCanvas.Projection;
            if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
            {
                double change = e.HorizontalChange;
                planeProjection.GlobalOffsetX += change;
            }
        }

        void OnDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            double change = e.HorizontalChange;
            //往左滑动
            if (change < 0)
            {
                _currentIndex++;
            }
            //往右滑动
            if (change > 0)
            {
                _currentIndex--;
            }
            if (_currentIndex > 5)
            {
                _currentIndex = 5;
            }
            if (_currentIndex < 0)
            {
                _currentIndex = 0;
            }
            PlayImageStoryBoard(_playSeconds);
            if (_currentIndex == 0)
            {
                Storyboard opSt = new Storyboard();
                DoubleAnimation opDa = CreateAnimation(this.HeartImage, FrameworkElement.OpacityProperty, 1.0, _playSeconds);
                opSt.Children.Add(opDa);
                opSt.Begin();
                PlayCanvasStoryBoard(this.HeartImage,149, 300, _playSeconds);
            }
            else if (_currentIndex == 1)
            {
                Storyboard st = PlayCanvasStoryBoard(this.HeartImage,635.0, 405.0, _playSeconds);
                st.Completed += (s, ev) =>
                    {
                        Storyboard opSt = new Storyboard();
                        DoubleAnimation opDa = CreateAnimation(this.HeartImage, FrameworkElement.OpacityProperty, 0.0, _playSeconds);
                        opSt.Children.Add(opDa);
                        opSt.Begin();
                    };
            }
            else if (_currentIndex == 2)
            {
                Storyboard opSt = new Storyboard();
                DoubleAnimation opDa = CreateAnimation(this.HeartImage, FrameworkElement.OpacityProperty, 1.0, _playSeconds);
                opSt.Children.Add(opDa);
                opSt.Begin();
                PlayCanvasStoryBoard(this.HeartImage,1106.0, 259.0, _playSeconds);
            }
            else if (_currentIndex == 3)
            {
                PlayCanvasStoryBoard(this.HeartImage,1584.0, 272.0, _playSeconds);
            }
            else if (_currentIndex == 4)
            {
                PlayCanvasStoryBoard(this.HeartImage,2060.0, 187.0, _playSeconds);
            }
            else if (_currentIndex == 5)
            {
                PlayCanvasStoryBoard(this.HeartImage,2530.0, 306.0, _playSeconds);
                if (!btnShow)
                {
                    this.button.Visibility = Visibility.Visible;
                    PlayKeyStoryBoard(this.button);
                    btnShow = true;
                }
            }
        }

        void OnFlick(object sender, FlickGestureEventArgs e)
        {
            
        }

        private void PlayImageStoryBoard(double seconds)
        {
            Storyboard st = new Storyboard();
            PlaneProjection planeProjection = (PlaneProjection)(this.ContainerCanvas.Projection);
            DoubleAnimation da = this.CreateAnimation(planeProjection, PlaneProjection.GlobalOffsetXProperty, -_currentIndex * 480, seconds);
            st.Children.Add(da);
            st.Begin();
        }

        private Storyboard PlayCanvasStoryBoard(DependencyObject obj,double toLeftValue,double toTopValue,double seconds) 
        {
            Storyboard st = new Storyboard();
            DoubleAnimation daLeft = CreateAnimation(obj, Canvas.LeftProperty, toLeftValue, seconds);
            DoubleAnimation daTop = CreateAnimation(obj, Canvas.TopProperty, toTopValue, seconds);
            st.Children.Add(daLeft);
            st.Children.Add(daTop);
            st.Begin();
            return st;
        }

        private Storyboard PlayKeyStoryBoard(DependencyObject obj)
        {
            Storyboard st = new Storyboard();

            DoubleAnimationUsingKeyFrames leftDau = new DoubleAnimationUsingKeyFrames();
            leftDau.BeginTime = TimeSpan.FromMilliseconds(0);
            leftDau.Duration = TimeSpan.FromMilliseconds(2500);

            EasingDoubleKeyFrame leftEdk = new EasingDoubleKeyFrame();
            leftEdk.KeyTime = TimeSpan.FromMilliseconds(500);
            leftEdk.Value = 2410;
            leftDau.KeyFrames.Add(leftEdk);
            EasingDoubleKeyFrame leftEdk1 = new EasingDoubleKeyFrame();
            leftEdk1.KeyTime = TimeSpan.FromMilliseconds(700);
            leftEdk1.Value = 2440;
            leftDau.KeyFrames.Add(leftEdk1);
            EasingDoubleKeyFrame leftEdk2 = new EasingDoubleKeyFrame();
            leftEdk2.KeyTime = TimeSpan.FromMilliseconds(1000);
            leftEdk2.Value = 2470;
            leftDau.KeyFrames.Add(leftEdk2);
            EasingDoubleKeyFrame leftEdk3 = new EasingDoubleKeyFrame();
            leftEdk3.KeyTime = TimeSpan.FromMilliseconds(1200);
            leftEdk3.Value = 2500;
            leftDau.KeyFrames.Add(leftEdk3);
            EasingDoubleKeyFrame leftEdk4 = new EasingDoubleKeyFrame();
            leftEdk4.KeyTime = TimeSpan.FromMilliseconds(1500);
            leftEdk4.Value = 2520;
            leftDau.KeyFrames.Add(leftEdk4);
            EasingDoubleKeyFrame leftEdk5 = new EasingDoubleKeyFrame();
            leftEdk5.KeyTime = TimeSpan.FromMilliseconds(1800);
            leftEdk5.Value = 2520;
            leftDau.KeyFrames.Add(leftEdk5);
            EasingDoubleKeyFrame leftEdk6 = new EasingDoubleKeyFrame();
            leftEdk6.KeyTime = TimeSpan.FromMilliseconds(2100);
            leftEdk6.Value = 2520;
            leftDau.KeyFrames.Add(leftEdk6);
            EasingDoubleKeyFrame leftEdk7 = new EasingDoubleKeyFrame();
            leftEdk7.KeyTime = TimeSpan.FromMilliseconds(2300);
            leftEdk7.Value = 2520;
            leftDau.KeyFrames.Add(leftEdk7);

            Storyboard.SetTarget(leftDau, obj);
            Storyboard.SetTargetProperty(leftDau, new PropertyPath("(Canvas.Left)"));
            st.Children.Add(leftDau);

            DoubleAnimationUsingKeyFrames topDau = new DoubleAnimationUsingKeyFrames();
            topDau.BeginTime = TimeSpan.FromMilliseconds(0);
            topDau.Duration = TimeSpan.FromMilliseconds(2500);
            EasingDoubleKeyFrame topEdk = new EasingDoubleKeyFrame();
            topEdk.KeyTime = TimeSpan.FromMilliseconds(500);
            topEdk.Value = 550;
            topDau.KeyFrames.Add(topEdk);
            EasingDoubleKeyFrame topEdk1 = new EasingDoubleKeyFrame();
            topEdk1.KeyTime = TimeSpan.FromMilliseconds(700);
            topEdk1.Value = 560;
            topDau.KeyFrames.Add(topEdk1);
            EasingDoubleKeyFrame topEdk2 = new EasingDoubleKeyFrame();
            topEdk2.KeyTime = TimeSpan.FromMilliseconds(1000);
            topEdk2.Value = 570;
            topDau.KeyFrames.Add(topEdk2);
            EasingDoubleKeyFrame topEdk3 = new EasingDoubleKeyFrame();
            topEdk3.KeyTime = TimeSpan.FromMilliseconds(1200);
            topEdk3.Value = 580;
            topDau.KeyFrames.Add(topEdk3);
            EasingDoubleKeyFrame topEdk4 = new EasingDoubleKeyFrame();
            topEdk4.KeyTime = TimeSpan.FromMilliseconds(1500);
            topEdk4.Value = 550;
            topDau.KeyFrames.Add(topEdk4);
            EasingDoubleKeyFrame topEdk5 = new EasingDoubleKeyFrame();
            topEdk5.KeyTime = TimeSpan.FromMilliseconds(1800);
            topEdk5.Value = 570;
            topDau.KeyFrames.Add(topEdk5);
            EasingDoubleKeyFrame topEdk6 = new EasingDoubleKeyFrame();
            topEdk6.KeyTime = TimeSpan.FromMilliseconds(2100);
            topEdk6.Value = 550;
            topDau.KeyFrames.Add(topEdk6);
            EasingDoubleKeyFrame topEdk7 = new EasingDoubleKeyFrame();
            topEdk7.KeyTime = TimeSpan.FromMilliseconds(2500);
            topEdk7.Value = 560;
            topDau.KeyFrames.Add(topEdk7);
            
            Storyboard.SetTarget(topDau, obj);
            Storyboard.SetTargetProperty(topDau, new PropertyPath("(Canvas.Top)"));
            st.Children.Add(topDau);

            st.Begin();
            return st;
        }


        private DoubleAnimation CreateAnimation(DependencyObject obj,DependencyProperty prop,double toValue,double seconds)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.Duration = TimeSpan.FromMilliseconds(seconds);
            da.From = Convert.ToDouble(obj.GetValue(prop));
            da.To = toValue;

            Storyboard.SetTarget(da,obj);
            Storyboard.SetTargetProperty(da, new PropertyPath(prop));
            return da;
        }
    }
}